﻿using System;
using System.Security.Cryptography;
using System.Text;
using Avalonia.Controls;
using Avalonia.Interactivity;

namespace iOSClub.App.Desktop.ToolPages.Generate;

public partial class HashGenerateView : UserControl
{
    public HashGenerateView()
    {
        InitializeComponent();
        TypeCombo.ItemsSource = new[] { "16进制", "Base64" };
        TypeCombo.SelectedItem = "16进制";
    }

    private void TextChanged(object? sender, TextChangedEventArgs e)
    {
        ConvertHash(InputBox.Text);
    }

    private void TypeSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        ConvertHash(InputBox.Text);
    }

    private void CharCheckedChanged(object? sender, RoutedEventArgs e)
    {
        ConvertHash(InputBox.Text);
    }

    private void ConvertHash(string? input)
    {
        if (string.IsNullOrEmpty(input))
        {
            MD5Box.Text = SHA1Box.Text = SHA256Box.Text = SHA384Box.Text = SHA512Box.Text = "";
            return;
        }

        if (TypeCombo.SelectedItem is not string s) return;
        var context = Encoding.UTF8.GetBytes(input);
        var md5 = MD5.HashData(context);
        var sha1 = SHA1.HashData(context);
        var sha256 = SHA256.HashData(context);
        var sha384 = SHA384.HashData(context);
        var sha512 = SHA512.HashData(context);
        if (s == "16进制")
        {
            MD5Box.Text = CharSwitch.IsChecked!.Value
                ? BitConverter.ToString(md5).Replace("-", "").ToUpper()
                : BitConverter.ToString(md5).Replace("-", "").ToLower();
            SHA1Box.Text = CharSwitch.IsChecked!.Value
                ? BitConverter.ToString(sha1).Replace("-", "").ToUpper()
                : BitConverter.ToString(sha1).Replace("-", "").ToLower();
            SHA256Box.Text = CharSwitch.IsChecked!.Value
                ? BitConverter.ToString(sha256).Replace("-", "").ToUpper()
                : BitConverter.ToString(sha256).Replace("-", "").ToLower();
            SHA384Box.Text = CharSwitch.IsChecked!.Value
                ? BitConverter.ToString(sha384).Replace("-", "").ToUpper()
                : BitConverter.ToString(sha384).Replace("-", "").ToLower();
            SHA512Box.Text = CharSwitch.IsChecked!.Value
                ? BitConverter.ToString(sha512).Replace("-", "").ToUpper()
                : BitConverter.ToString(sha512).Replace("-", "").ToLower();
        }
        else
        {
            MD5Box.Text = CharSwitch.IsChecked!.Value
                ? Convert.ToBase64String(md5).ToUpper()
                : Convert.ToBase64String(md5).ToLower();
            SHA1Box.Text = CharSwitch.IsChecked!.Value
                ? Convert.ToBase64String(sha1).ToUpper()
                : Convert.ToBase64String(sha1).ToLower();
            SHA256Box.Text = CharSwitch.IsChecked!.Value
                ? Convert.ToBase64String(sha256).ToUpper()
                : Convert.ToBase64String(sha256).ToLower();
            SHA384Box.Text = CharSwitch.IsChecked!.Value
                ? Convert.ToBase64String(sha384).ToUpper()
                : Convert.ToBase64String(sha384).ToLower();
            SHA512Box.Text = CharSwitch.IsChecked!.Value
                ? Convert.ToBase64String(sha512).ToUpper()
                : Convert.ToBase64String(sha512).ToLower();
        }
    }

    private void MD5Click(object? sender, RoutedEventArgs e)
    {
        if(string.IsNullOrEmpty(MD5Box.Text))return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(MD5Box.Text);
    }

    private void SHA1Click(object? sender, RoutedEventArgs e)
    {
        if(string.IsNullOrEmpty(SHA1Box.Text))return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(SHA1Box.Text);
    }

    private void SHA256Click(object? sender, RoutedEventArgs e)
    {
        if(string.IsNullOrEmpty(SHA256Box.Text))return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(SHA256Box.Text);
    }

    private void SHA384Click(object? sender, RoutedEventArgs e)
    {
        if(string.IsNullOrEmpty(SHA384Box.Text))return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(SHA384Box.Text);
    }

    private void SHA512Click(object? sender, RoutedEventArgs e)
    {
        if(string.IsNullOrEmpty(SHA512Box.Text))return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(SHA512Box.Text);
    }
}